package com.sztx.pay.center.common.yhbf;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class YHBFUtil {
	private static final String DEFAULT_KEY = "ba1dc1ea0cde5f03";
	private static final String DEFAULT_ENCODE = Charset.defaultCharset().name();
	
	public static String encrypt(String plainText, String keyTex, String encode) throws UnsupportedEncodingException
	{
		byte[] result = process(plainText, keyTex, encode, Cipher.ENCRYPT_MODE);
		return Base64.encodeBase64String(result);
		
	}
	
	public static String encrypt(String plainText, String keyTex) throws UnsupportedEncodingException
	{
		return encrypt(plainText, keyTex, DEFAULT_ENCODE);
	}
	
	public static String encryptEncode(String plainText, String encode) throws UnsupportedEncodingException
	{
		return encrypt(plainText, DEFAULT_KEY, encode);
	}
	
	public static String encrypt(String plainText) throws UnsupportedEncodingException
	{
		return encrypt(plainText, DEFAULT_KEY);
	}
	
	
	
	public static String decrypt(String cipherText, String keyText, String encode) throws UnsupportedEncodingException
	{
		byte[] result = process(cipherText, keyText, encode, Cipher.DECRYPT_MODE);
				
		return new String(result, encode).trim();
	}
	
	public static String decrypt(String cipherText, String keyText) throws UnsupportedEncodingException
	{
		return decrypt(cipherText, keyText, DEFAULT_ENCODE);
	}
	
	public static String decryptEncode(String cipherText, String encode) throws UnsupportedEncodingException
	{
		return decrypt(cipherText, DEFAULT_KEY, encode);
	}
	
	
	public static String decrypt(String cipherText) throws UnsupportedEncodingException
	{
		return decrypt(cipherText, DEFAULT_KEY);
	}

	public static byte[] encrypt(byte[] data, byte[] key) {
		return process(data, key, Cipher.ENCRYPT_MODE);
	}

	public static  byte[] decrypt(byte[] data, byte[] key) {
		return process(data, key, Cipher.DECRYPT_MODE);
	}
	
	
	private static byte[] process(String text, String keyTex, String encode, int type) throws UnsupportedEncodingException
	{
		byte[] data = null;
		
		if (type == Cipher.ENCRYPT_MODE) {
			data = text.getBytes(encode);
		}
		else {
			data = Base64.decodeBase64(text);
			
		}
		
		byte[] key = keyTex.getBytes(encode);
		
		return process(data, key, type);
	}

	private static byte[] process(byte[] data, byte[] key, int type) {
		BlowFish blowfish = new BlowFish();
		int len = data.length;
		byte[] out = new byte[len + 8];
		if (type == Cipher.ENCRYPT_MODE) {
			blowfish.init(true, key);
			if (len % 8 != 0) {
				
				byte[] tmp = new byte[(len / 8 + 1) * 8];
				System.arraycopy(data, 0, tmp, 0, data.length);
				data = tmp;
			}
		} else {
			blowfish.init(false, key);
		}

		int size = data.length / 8;

		int offset = 0;
		for (int i = 0; i < size; i++) {
			offset = i * 8;
			blowfish.transformBlock(data, offset, out, offset);
			blowfish.reset();
		}
		
		byte[] result = new byte[offset + 8];
		System.arraycopy(out, 0, result, 0, offset + 8);

		return result;
	}

}
